#!/bin/bash
# DJVU_to_Monochrome_DJVU_1.sh ( 35 )
File1="$1" ;
[ "x$TMPDIR" == x ] && TMPDIR=/tmp
tmpname="$TMPDIR/Electric_Document_$$.tmp"
mkdir -p "$tmpname"
Page_Count_1=`djvudump $1 | awk '{ count = 0 } /pages/ { for(i=2;i<=NF;i++){ if($i ~ "pages"){b=i-1; print $b}}}'` ;
File1="$1" ;
Start1=1 ;
End1=$Page_Count_1 ;
RootBindName1="$2" ;
# ColorManagement1=$3 ;
ColorManagement1=1 ;
# Scale1=$4 ;
Scale1=1 ;
RotationLandscape1=1 ;
PWidth2=4950 ;
PHeight2=6450 ;
Width3=5100 ;
Height3=6600 ;
EnableTransform1=0 ;
JBigPath1=/tmp/agl-jbig2enc-c92abf1 ;
OutputFormat1="djvu" ;

# Input File :
# File1=Input_1.djvu ;

# Page Range :
# Start1=1 ;
# End1=1 ;

# Color Interpretation ( 1 for intelligent flattening , 2 for half-toning )
# ColorManagement1=1 ;

# Size of Cropping Frame ( before rotation ) :
# Width2=4800 ;
# Height2=6300 ;

# Size of Output Page
# Width3=5100 ;
# Height3=6600 ;

# Offset Method for Input ( 1 for automatic centering , 2 for custom offsets , 3 for separate offsets for even and odd pages ) :
IOffsetMethod1=1 ;

# Offset Method for Output ( 1 for automatic centering , 2 for custom offsets , 3 for separate offsets for even and odd pages ) :
OOffsetMethod1=1 ;

# Note that , if one of the following two options ( but no the All Pages option ) the offsets are adapted so that the effect is that of rotating the page before cropping it . This allows and is explicitly designed for the correction or print preparation of documents in which certain pages are rotated for the display of certain figures . A rotation value of 2 is meaningless for either of the two options but is coded all the same .

# Rotation for Pages in Portrait Orientation ( 0 for no , 1 for one-quarter turn ( counterclockwise ) , 2 for half turn , 3 for three-quarters turn ) :
RotationPortrait1=0 ;

# Rotation for Pages in Landscape Orientation ( 0 for no , 1 for one-quarter turn ( counterclockwise ) , 2 for half turn , 3 for three-quarters turn ) :
RotationLandscape1=0 ;

# This option may not operate properly if either of the previous two settings is non-zero . Luckily , there is absolutely no practical reason for such an arrangement .

# Rotation for All Pages ( 0 for no , 1 for one-quarter turn ( counterclockwise ) , 2 for half turn , 3 for three-quarters turn ) :
RotationAll1=0 ;

# Input Resolution :
# InputResolution1=600 ;
InputResolution1=`djvudump "$1" | grep '^[ \t]*INFO \[10\][ \t]*DjVu [0-9]*x[0-9]*\, [A-Za-z0-9]*\, [0-9]* dpi' | sed -e 's/^[ \t]*INFO \[10\][ \t]*DjVu \([0-9]*\)x\([0-9]*\)\, \([A-Za-z0-9]*\)\, \([0-9]*\) dpi[\,A-Za-z0-9 \t\.=]*$/\1 \2 \3 \4/g' | head -n 1 | awk '{ printf "%s" , $4 ; }'` ;

# General Input Offsets ( only for custom offsets ; before rotation ) :
IXOffset1=600 ;
IYOffset1=600 ;

# Input Offsets for Odd Pages ( only for separate custom offsets ; before rotation ) :
IXOffsetOdd1=600 ;
IYOffsetOdd1=600 ;

# Input Offsets for Even Pages ( only for separate custom offsets ; before rotation ) :
IXOffsetEven1=600 ;
IYOffsetEven1=600 ;

# General Output Offsets ( only for custom offsets ; before rotation ) :
OXOffset1=600 ;
OYOffset1=600 ;

# Output Offsets for Odd Pages ( only for separate custom offsets ; before rotation ) :
OXOffsetOdd1=600 ;
OYOffsetOdd1=600 ;

# Output Offsets for Even Pages ( only for separate custom offsets ; before rotation ) :
OXOffsetEven1=600 ;
OYOffsetEven1=600 ;

# Output Options :
# OutputResolution1=600 ;
OutputResolution1=$InputResolution1 ;
MakeLossless1=1 ;
MakeClean1=0 ;
MakeLossy1=0 ;
Bind1=1 ;
# RootBindName1=Output_1 ;
RemovePageFiles1=1 ;

# Processing Options ( This need not end in a forward slash . ) :
PageFileDirectory1="$TMPDIR/Electric_Document_$$.tmp" ;

for Page1 in `seq $Start1 $End1`
  do
	  # Loc1="`Number_Crop_1 4 $Page1`" ;
	  Loc1=`printf "%04d" $Page1` ;
          Filebase1="$PageFileDirectory1"/"Page_"`printf "%04d" $Page1`
	  File5="$PageFileDirectory1"/"Page_$Loc1.C.djvu" ;
	  File6="$PageFileDirectory1"/"Page_$Loc1.Cl.djvu" ;
	  File7="$PageFileDirectory1"/"Page_$Loc1.L.djvu" ;
	  Width2=$PWidth2 ;
	  Height2=$PHeight2 ;
	  if [[ $ColorManagement1 = 1 ]] ;
	  then
	    ddjvu -format=pbm -mode=black -page=$Page1 "$File1" "$PageFileDirectory1"/Temp_0.pbm ;
	    if [[ $Scale1 = 1 ]] ;
	    then
	      cp "$PageFileDirectory1"/Temp_0.pbm "$PageFileDirectory1"/Temp_1.pbm ;
	    else
	      pnmscalefixed $Scale1 "$PageFileDirectory1"/Temp_0.pbm | pgmtopbm -threshold > "$PageFileDirectory1"/Temp_1.pbm ;
	    fi
	    rm "$PageFileDirectory1"/Temp_0.pbm ;
	  elif [[ $ColorManagement1 = 2 ]] ;
	  then
	    ddjvu -format=ppm -mode=color -page=$Page1 "$File1" "$PageFileDirectory1"/Temp_0.ppm ;
	    if [[ $Scale1 = 1 ]] ;
	    then
	      ppmtopgm "$PageFileDirectory1"/Temp_0.ppm | pgmtopbm > "$PageFileDirectory1"/Temp_1.pbm ;
	    else
	      ppmtopgm "$PageFileDirectory1"/Temp_0.ppm | pnmscalefixed $Scale1 | pgmtopbm > "$PageFileDirectory1"/Temp_1.pbm ;
	    fi
	    rm "$PageFileDirectory1"/Temp_0.ppm ;
	  else
	    ddjvu -format=pbm -mode=black -page=$Page1 "$File1" "$PageFileDirectory1"/Temp_0.pbm ;
	    if [[ $Scale1 = 1 ]] ;
	    then
	      cp "$PageFileDirectory1"/Temp_0.pbm "$PageFileDirectory1"/Temp_1.pbm ;
	    else
	      pnmscalefixed $Scale1 "$PageFileDirectory1"/Temp_0.pbm | pgmtopbm -threshold > "$PageFileDirectory1"/Temp_1.pbm ;
	    fi
	    rm "$PageFileDirectory1"/Temp_0.pbm ;
	  fi
	  # For manipulation of colour images , comment the previous line and uncomment ( and possibly modify ) some of the other lines .
	  # ddjvu -format=ppm -mode=color -page=$Page1 $File1 Temp_1.ppm ;
	  # The following line performs a simple colour flattening and thresholding operation . This is good for text printed in one non-black colour . This is very bad for photographs .
	  # ppmtopgm Temp_1.ppm | pgmtopbm -threshold > ./Temp_1.pbm
	  # The following line assigns distinct gray levels to differing colours  and then dithers . This is good for diagrams in which one must distinguish colours . This is very bad and extremely slow for photographs .
	  # ppmdist Temp_1.ppm | pgmtopbm -floyd > ./Temp_1.pbm
	  # The following line performs a simple colour flattening operation and then dithers . This ought to work for most photographs .
	  # ppmtopgm Temp_1.ppm | pgmtopbm -floyd > ./Temp_1.pbm
	  # The following line removes the colour buffer file , which tends to be extremely large .
	  # rm Temp_1.ppm
	  Width1=`PBM_Info_1 --Width < "$PageFileDirectory1"/Temp_1.pbm` ;
	  Height1=`PBM_Info_1 --Height < "$PageFileDirectory1"/Temp_1.pbm` ;
  if [ $EnableTransform1 -eq 1 ] ;
  then
	  if [[ $IOffsetMethod1 = 2 ]] ;
	  then  # The offsets are set manually .
	    let "XOffset1 = IXOffset1" ;
	    let "YOffset1 = IYOffset1" ;
	  elif [[ $IOffsetMethod1 = 3 ]] ;
	  then # The offsets are set manually and separately .
	    if [[ $(( $Page1 % 2 )) = 1 ]] ;
	    then # The page number is odd .
	      let "XOffset1 = IXOffsetOdd1" ;
	      let "YOffset1 = IYOffsetOdd1" ;
	    else # The page number is non-odd and , hopefully , even .
	      let "XOffset1 = IXOffsetEven1" ;
	      let "YOffset1 = IYOffsetEven1" ;
	    fi
	  else # The offsets are set automatically .
	    if [[ $Height1 > $Width1 ]] ;
	    then # The page is in portrait orientation .
	      if [[ $RotationPortrait1 = 1 ]] || [[ $RotationPortrait1 = 3 ]] ;
		then # The dimensions and offsets need swapping since orientation changes .
		if [[ $Width1 < $Height2 ]] ; then
		  Height2=$Width1 ;
		fi ;
		if [[ $Height1 < $Width2 ]] ; then
		  Width2=$Height1 ;
		fi ;
		let "XOffset1 = ( ( Width1 - Height2 ) / 2 )" ;
		let "YOffset1 = ( ( Height1 - Width2 ) / 2 )" ;
		if [[ $XOffset1 < 0 ]]
		then
		  XOffset1=0 ;
		  Width2=$Height1 ;
		fi
		if [[ $YOffset1 < 0 ]] ;
		then
		  YOffset1=0 ;
		  Height2=$Width1 ;
		fi
	      else # The dimensions and offsets do not need swapping since orientation persists .
		if [[ $Width1 < $Width2 ]] ; then
		  Width2=$Width1 ;
		fi ;
		if [[ $Height1 < $Height2 ]] ; then
		  Height2=$Height1 ;
		fi ;
		let "XOffset1 = ( ( Width1 - Width2 ) / 2 )" ;
		let "YOffset1 = ( ( Height1 - Height2 ) / 2 )" ;
		if [[ $XOffset1 < 0 ]] ;
		then
		  XOffset1=0 ;
		  Width2=$Width1 ;
		fi
		if [[ $YOffset1 < 0 ]] ;
		then
		  YOffset1=0 ;
		  Height2=$Height1 ;
		fi
	      fi
	    elif [[ $Height1 < $Width1 ]] ;
	    then # The page is in landscape orientation .
	      if [[ $RotationLandscape1 = 1 ]] || [[ $RotationLandscape1 = 3 ]] ;
		then # The dimensions and offsets need swapping since orientation changes .
		if [[ $Width1 < $Height2 ]] ; then
		  Height2=$Width1 ;
		fi ;
		if [[ $Height1 < $Width2 ]] ; then
		  Width2=$Height1 ;
		fi ;
		let "XOffset1 = ( ( Width1 - Height2 ) / 2 )" ;
		let "YOffset1 = ( ( Height1 - Width2 ) / 2 )" ;
		if [[ $XOffset1 < 0 ]] ;
		then
		  XOffset1=0 ;
		  Width2=$Height1 ;
		fi
		if [[ $YOffset1 < 0 ]] ;
		then
		  YOffset1=0 ;
		  Height2=$Width1 ;
		fi
	      else # The dimensions and offsets do not need swapping since orientation persists .
		if [[ $Width1 < $Width2 ]] ; then
		  Width2=$Width1 ;
		fi ;
		if [[ $Height1 < $Height2 ]] ; then
		  Height2=$Height1 ;
		fi ;
		let "XOffset1 = ( ( Width1 - Width2 ) / 2 )" ;
		let "YOffset1 = ( ( Height1 - Height2 ) / 2 )" ;
		if [[ $XOffset1 < 0 ]] ;
		then
		  XOffset1=0 ;
		  Width2=$Width1 ;
		fi
		if [[ $YOffset1 < 0 ]] ;
		then
		  YOffset1=0 ;
		  Height2=$Height1 ;
		fi
	      fi
	    else # The page is square .
		let "XOffset1 = ( ( Width1 - Width2 ) / 2 )" ;
		let "YOffset1 = ( ( Height1 - Height2 ) / 2 )" ;
		if [[ $Width1 < $Width2 ]] ; then
		  Width2=$Width1 ;
		fi ;
		if [[ $Height1 < $Height2 ]] ; then
		  Height2=$Height1 ;
		fi ;
		if [[ $XOffset1 < 0 ]] ;
		then
		  XOffset1=0 ;
		  Width2=$Width1 ;
		fi
		if [[ $YOffset1 < 0 ]] ;
		then
		  YOffset1=0 ;
		  Height2=$Height1 ;
		fi
	    fi
	  fi
	  if [[ $OOffsetMethod1 = 2 ]] ;
	  then  # The offsets are set manually .
	    let "XOffset2 = OXOffset1" ;
	    let "YOffset2 = OYOffset1" ;
	  elif [[ $IOffsetMethod1 = 3 ]] ;
	  then # The offsets are set manually and separately .
	    if [[ $(( $Page1 % 2 )) = 1 ]] ;
	    then # The page number is odd .
	      let "XOffset2 = OXOffsetOdd1" ;
	      let "YOffset2 = OYOffsetOdd1" ;
	    else
	      let "XOffset2 = OXOffsetEven1" ;
	      let "YOffset2 = OYOffsetEven1" ;
	    fi
	  else # The offsets are set automatically .
	    if [[ $Height1 > $Width1 ]] ;
	    then # The page is in portrait orientation .
	      # if [[ $RotationPortrait1 = 1 ]] || [[ $RotationPortrait1 = 3 ]] ;
	      #   then # The dimensions and offsets need swapping since orientation changes .
	      #   let "XOffset2 = ( ( Width3 - Height2 ) / 2 )" ;
	      #   let "YOffset2 = ( ( Height3 - Width2 ) / 2 )" ;
	      # else # The dimensions and offsets do not need swapping since orientation persists .
	      #   let "XOffset2 = ( ( Width3 - Width2 ) / 2 )" ;
	      #   let "YOffset2 = ( ( Height3 - Height2 ) / 2 )" ;
	      # fi
	      let "XOffset2 = ( ( Width3 - Width2 ) / 2 )" ;
	      let "YOffset2 = ( ( Height3 - Height2 ) / 2 )" ;
	    elif [[ $Height1 < $Width1 ]] ;
	    then # The page is in landscape orientation .
	      # if [[ $RotationLandscape1 = 1 ]] || [[ $RotationLandscape1 = 3 ]] ;
	      #   then # The dimensions and offsets need swapping since orientation changes .
	      #   let "XOffset2 = ( ( Width3 - Height2 ) / 2 )" ;
	      #   let "YOffset2 = ( ( Height3 - Width2 ) / 2 )" ;
	      # else # The dimensions and offsets do not need swapping since orientation persists .
	      #   let "XOffset2 = ( ( Width3 - Width2 ) / 2 )" ;
	      #   let "YOffset2 = ( ( Height3 - Height2 ) / 2 )" ;
	      # fi
	      let "XOffset2 = ( ( Width3 - Width2 ) / 2 )" ;
	      let "YOffset2 = ( ( Height3 - Height2 ) / 2 )" ;
	    else # The page is square .
		let "XOffset2 = ( ( Width3 - Width2 ) / 2 )" ;
		let "YOffset2 = ( ( Height3 - Height2 ) / 2 )" ;
	    fi
	  fi
	  echo $Page1
	  if [[ $Height1 > $Width1 ]] ;
	  then # The page is in portrait orientation .
	    if [[ $RotationPortrait1 = 1 ]] ;
	    then
	      let "XOffsetRev1 = $Width1 - $YOffset1 - $Height2" ;
	      let "YOffsetRev1 = $XOffset1" ;
	      if [[ $XOffsetRev1 < 0 ]] ;
	      then
		let "XOffsetRev1 = 0" ;
	      fi
	      if [[ $YOffsetRev1 < 0 ]] ;
	      then
		let "YOffsetRev1 = 0" ;
	      fi
	      PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Width2 --YTotal1 $Height2 --XTotal2 $Width2 --YTotal2 $Height2 --Rotate 1 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    elif [[ $RotationPortrait1 = 2 ]] ;
	    then
	      let "XOffsetRev1 = $Width1 - $XOffset1 - $Width2" ;
	      let "YOffsetRev1 = $Height1 - $XOffset1 - $Height2" ;
	      if [[ $XOffsetRev1 < 0 ]] ;
	      then
		let "XOffsetRev1 = 0" ;
	      fi
	      if [[ $YOffsetRev1 < 0 ]] ;
	      then
		let "YOffsetRev1 = 0" ;
	      fi
	      PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Width2 --YTotal1 $Height2 --XTotal2 $Width2 --YTotal2 $Height2 --Rotate 2 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    elif [[ $RotationPortrait1 = 3 ]] ;
	    then
	      let "XOffsetRev1 = $YOffset1" ;
	      let "YOffsetRev1 = $Height1 - $XOffset1 - $Width2" ;
	      if [[ $XOffsetRev1 < 0 ]] ;
	      then
		let "XOffsetRev1 = 0" ;
	      fi
	      if [[ $YOffsetRev1 < 0 ]] ;
	      then
		let "YOffsetRev1 = 0" ;
	      fi
	      PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Height2 --YTotal1 $Width2 --XTotal2 $Width2 --YTotal2 $Height2 --Rotate 3 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    elif [[ $RotationAll1 > 0 ]] ; # This is not written correctly at the moment .
	    then
	      PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Width2 --YTotal1 $Height2 --XTotal2 $Width2 --YTotal2 $Height2 --Rotate $RotationAll1 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    else
	    PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Width2 --YTotal1 $Height2 --XTotal2 $Width2 --YTotal2 $Height2 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    fi
	  elif [[ $Width1 > $Height1 ]] ;
	  then # The page is in landscape orientation .
	    if [[ $RotationLandscape1 = 1 ]] ;
	    then
	      let "XOffsetRev1 = $Width1 - $YOffset1 - $Height2" ;
	      let "YOffsetRev1 = $XOffset1" ;
	      if [[ $XOffsetRev1 < 0 ]] ;
	      then
		let "XOffsetRev1 = 0" ;
	      fi
	      if [[ $YOffsetRev1 < 0 ]] ;
	      then
		let "YOffsetRev1 = 0" ;
	      fi
	      PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Height2 --YTotal1 $Width2 --XTotal2 $Width2 --YTotal2 $Height2 --Rotate 1 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    elif [[ $RotationLandscape1 = 2 ]] ;
	    then
	      let "XOffsetRev1 = $Width1 - $XOffset1 - $Width2" ;
	      let "YOffsetRev1 = $Height1 - $XOffset1 - $Height2" ;
	      if [[ $XOffsetRev1 < 0 ]] ;
	      then
		let "XOffsetRev1 = 0" ;
	      fi
	      if [[ $YOffsetRev1 < 0 ]] ;
	      then
		let "YOffsetRev1 = 0" ;
	      fi
	      PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Height2 --YTotal1 $Width2 --XTotal2 $Width2 --YTotal2 $Height2 --Rotate 2 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    elif [[ $RotationLandscape1 = 3 ]] ;
	    then
	      let "XOffsetRev1 = $YOffset1" ;
	      let "YOffsetRev1 = $Height1 - $XOffset1 - $Width2" ;
	      if [[ $XOffsetRev1 < 0 ]] ;
	      then
		let "XOffsetRev1 = 0" ;
	      fi
	      if [[ $YOffsetRev1 < 0 ]] ;
	      then
		let "YOffsetRev1 = 0" ;
	      fi
	      PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Height2 --YTotal1 $Width2 --XTotal2 $Width2 --YTotal2 $Height2 --Rotate 3 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    elif [[ $RotationAll1 > 0 ]] ;
	    then
	      PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Width2 --YTotal1 $Height2 --XTotal2 $Width2 --YTotal2 $Height2 --Rotate $RotationAll1 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    else
	    PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Width2 --YTotal1 $Height2 --XTotal2 $Width2 --YTotal2 $Height2 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    fi
	  else
	    if [[ $RotationAll1 > 0 ]] ;
	    then
	      PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Width2 --YTotal1 $Height2 --XTotal2 $Width2 --YTotal2 $Height2 --Rotate $RotationAll1 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    else
	      PBM_Processor_1 --ManualDim2 1 --Width2 $Width3 --Height2 $Height3 --XOffset1 $XOffset1 --YOffset1 $YOffset1 --XOffset2 $XOffset2 --YOffset2 $YOffset2 --XTotal1 $Width2 --YTotal1 $Height2 --XTotal2 $Width2 --YTotal2 $Height2 < "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
	    fi
	  fi
  else
	cat "$PageFileDirectory1"/Temp_1.pbm > "$PageFileDirectory1"/Temp_2.pbm ;
  fi
  if [[ $MakeLossless1 = 1 ]] ;
  then
    cjb2 -dpi $OutputResolution1 "$PageFileDirectory1"/Temp_2.pbm "$File5" ;
    if [ "$OutputFormat1" != "djvu" ] ;
	then
	pnmtotiff -g4 -xresolution "$OutputResolution1" -yresolution "$OutputResolution1" "$PageFileDirectory1"/Temp_2.pbm > "$Filebase1".tiff ;
    fi ;
  fi
  if [[ $MakeClean1 = 1 ]] ;
  then
    cjb2 -dpi $OutputResolution1 -clean "$PageFileDirectory1"/Temp_2.pbm "$File6" ;
  fi
  if [[ $MakeLossy1 = 1 ]] ;
  then
    cjb2 -dpi $OutputResolution1 -lossy "$PageFileDirectory1"/Temp_2.pbm "$File7" ;
  fi
  rm "$PageFileDirectory1"/Temp_1.pbm ;
  rm "$PageFileDirectory1"/Temp_2.pbm ;
done
if [[ $Bind1 = 1 ]] ;
then
  if [[ $MakeLossless1 = 1 ]] ;
  then
	if [ "$OutputFormat1" = "pdf" ] ;
	then
		"$JBigPath1"/src/jbig2 -b "$PageFileDirectory1"/ -p "$PageFileDirectory1"/Page_????.tiff && "$JBigPath1"/pdf.py "$PageFileDirectory1"/ > "$RootBindName1" ;
	else
    # djvm -c "$RootBindName1".C.djvu "$PageFileDirectory1"/"Page_"*.Cr.djvu ;
    # DJVU_Monochrome_PostScript_1.sh "$RootBindName1".C.djvu "$RootBindName1".C.djvu.1_Print_1.ps ;
    # djvudigital --dpi=600 "$RootBindName1".C.djvu.1_Print_1.ps "$RootBindName1".C.djvu.1_Print_1.ps.1.djvu ;
    djvm -c "$RootBindName1" "$PageFileDirectory1"/"Page_"*.C.djvu ;
	fi ;
  fi
  if [[ $MakeClean1 = 1 ]] ;
  then
    true ;
    # djvm -c "$RootBindName1".Cl.djvu "$PageFileDirectory1"/"Page_"*.Cl.djvu ;
    # DJVU_Monochrome_PostScript_1.sh "$RootBindName1".Cl.djvu "$RootBindName1".Cl.djvu.1_Print_1.ps ;
    # djvudigital --dpi=600 "$RootBindName1".Cl.djvu.1_Print_1.ps "$RootBindName1".Cl.djvu.1_Print_1.ps.1.djvu ;
  fi
  if [[ $MakeLossy1 = 1 ]] ;
  then
    true ;
    djvm -c "$RootBindName1".L.djvu "$PageFileDirectory1"/"Page_"*.L.djvu ;
    # DJVU_Monochrome_PostScript_1.sh "$RootBindName1".L.djvu "$RootBindName1".L.djvu.1_Print_1.ps ;
    # djvudigital --dpi=600 "$RootBindName1".L.djvu.1_Print_1.ps "$RootBindName1".L.djvu.1_Print_1.ps.1.djvu ;
  fi
fi
if [[ $RemovePageFiles1 = 1 ]] ;
then
  rm "$PageFileDirectory1"/Page_*.*.djvu ;
fi

